package assignmentonepenguins

import org.junit.Test

class ListingIntegrationTests {
    @Test
    void test_BiddingSavedOnListingSave() {
        // Test when a listing is saved, any new bids added for the listing must be saved (B-6)
        Customer seller123 = new Customer(email: 'seller123@gmail.com', password: 'abc123')
        seller123.save(flush: true)
        Customer bidder = new Customer(email: 'bidder123@gmail.com', password: 'abc123')
        bidder.save(flush: true)
        Listing someListing = new Listing()
        someListing.setName('A Brand New Camera')
        someListing.setEndDate(new Date() + 7)
        someListing.setStartBid(149.99)
        someListing.setSeller(seller123)
        someListing.setCreatedBy("createdBy")
        someListing.save(flush: true)

        // Listing has been saved with no Bids
        assert someListing.getId() == 1
        assert someListing.getBiddings() == null

        // Create two bids for that listing, but do not save them to the database
        Bidding bid1 = new Bidding()
        bid1.setAmount(150)
        bid1.setBidder(bidder)
        bid1.setListing(someListing)
        Bidding bid2 = new Bidding()

        // Add the Biddings to the Listing and save the Listing to the database
        Set<Bidding> bidSet = new HashSet<Bidding>()
        bidSet.add(bid1)
        someListing.setBiddings(bidSet)

        // Verify Bid1 has not been saved to the database
        assert bid1.getId() == null

        // Save the Listing to the database again
        someListing.save(flush: true)
        assert someListing.getBiddings().size() == 1

        // Verify that the bid has now been saved to the database (B-6)
        assert bid1.getId() != null
    }

    @Test
    void test_MinimumBid() {
        // Test when a listing is saved, any new bids added for the listing must be saved (B-6)
        Customer seller234 = new Customer(email: 'seller234@gmail.com', password: '123abc', username: "SomeSeller")
        seller234.save(flush: true)
        Customer aBidder = new Customer(email: 'bidder234@gmail.com', password: '123abc', username: "SomeBidder")
        aBidder.save(flush: true)
        Listing aListing = new Listing()
        aListing.setName('A Brand New Camera')
        aListing.setEndDate(new Date() + 7)
        aListing.setStartBid(149.99)
        aListing.setSeller(seller234)
        aListing.setCreatedBy("createdBy")
        aListing.save(flush: true)

        // Listing has been saved with no Bids
        assert aListing.getId() == 1
        assert aListing.getBiddings() == null

        // Create two bids for that listing, but do not save them to the database
        Bidding aBid = new Bidding()
        aBid.setAmount(150)
        aBid.setListing(aListing)

        // Add the Biddings to the Listing and save the Listing to the database
        Set<Bidding> bidSet = new HashSet<Bidding>()
        bidSet.add(aBid)
        aListing.setBiddings(bidSet)

        // Verify Bid1 has not been saved to the database
        assert bid1.getId() == null

        // Save the Listing to the database again
        someListing.save(flush: true)
        assert aListing.getBiddings().size() == 1

        // Verify that the bid has now been saved to the database (B-6)
        assert bid1.getId() != null

        assert getMinBid(aListing.id) == aListing.sort {x, y -> y.amount.compareTo(x.amount)}.get(0).amount + 0.5
    }

    def getMinBid(long listingId) {
        Listing aListing = Listing.findById(listingId)
        double minBid = aListing.getBiddings().sort {x, y -> y.amount.compareTo(x.amount)}.get(0).amount + 0.5
        return minBid
    }
}